package org.mv.mm.docs;

import java.awt.BorderLayout;
import javax.swing.ActionMap;
import javax.swing.JTable;
import javax.swing.table.TableColumnModel;
import org.mv.mm.api.data.IDataProvider;
import org.mv.mm.docs.nodes.DocNode;
import org.mv.mm.docs.nodes.DocRootNode;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.OutlineView;
import org.openide.util.Lookup;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.TopComponent;

/**
 * Top component which displays something.
 */
@ConvertAsProperties(dtd = "-//org.mv.mm.docs//Docs//EN",
autostore = false)
@TopComponent.Description(preferredID = "DocsTopComponent",
//iconBase="SET/PATH/TO/ICON/HERE", 
persistenceType = TopComponent.PERSISTENCE_ONLY_OPENED)
@Messages({
  "CTL_DocsTopComponent=Documents",
  "HINT_DocsTopComponent=This is list of all documents."
})
public final class DocsTopComponent extends TopComponent implements ExplorerManager.Provider {

  private ExplorerManager em = new ExplorerManager();
  private OutlineView ov;

  public DocsTopComponent() {//TODO rozdělit na privátní metody, aby nebyl konstruktor tak dlouhý
    initComponents();
    setName(Bundle.CTL_DocsTopComponent());
    setToolTipText(Bundle.HINT_DocsTopComponent());

    IDataProvider dataProvider = Lookup.getDefault().lookup(IDataProvider.class);

    DocRootNode node = new DocRootNode(dataProvider);
    em.setRootContext(node);

    setLayout(new BorderLayout());
    ov = new OutlineView("Document title/working title");
    ov.setPropertyColumns(DocNode.ABBREV_PROPERTY, "Abbrev.");
    


    add(ov, BorderLayout.CENTER);
    
    ActionMap map = getActionMap();
    map.put("delete", ExplorerUtils.actionDelete(em, true)); // NOI18N
    /*
     * map.put(DefaultEditorKit.copyAction, ExplorerUtils.actionCopy(em)); //
     * currently unused map.put(DefaultEditorKit.cutAction,
     * ExplorerUtils.actionCut(em)); map.put(DefaultEditorKit.pasteAction, ExplorerUtils.actionPaste(em));
     */

    associateLookup(ExplorerUtils.createLookup(em, map));
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
  @Override
  public void componentOpened() {
    // TODO add custom code on component opening
  }

  @Override
  public void componentClosed() {
    // TODO add custom code on component closing
  }

  @Override
  protected void componentActivated() {
    ExplorerUtils.activateActions(em, true);
  }

  @Override
  protected void componentDeactivated() {
    ExplorerUtils.activateActions(em, false);
  }

  void writeProperties(java.util.Properties p) {
    // better to version settings since initial version as advocated at
    // http://wiki.apidesign.org/wiki/PropertyFiles
    p.setProperty("version", "1.0");
    
    // TODO store your settings
  }

  void readProperties(java.util.Properties p) {
    String version = p.getProperty("version");
    // TODO read your settings according to their version
  }

  @Override
  public ExplorerManager getExplorerManager() {
    return em;
  }
}
